﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using DevExpress.Web.ASPxGridView;
using System.Data;

public partial class Teacher_ManageTeacher : System.Web.UI.Page
{
    SqlConnection conn;
    DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Teacher"] == null)
        {
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            string user = Session["Teacher"].ToString();
            //Neu Giao vien da login
            if (user[0] == 'T' || user[0] == 't')
            {
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Institte_of_Fine_ArtsConnectionString"].ConnectionString);
                if(conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                //SqlCommand comm = new SqlCommand("Select PostingID From Posting Where 
            }//Neu Giao vien chua login
            else
            {
                Response.Redirect("~/Default.aspx");
            }
        }
        
    }

    void AddError(Dictionary<GridViewColumn, string> errors, GridViewColumn column, string errorText)
    {
        if (errors.ContainsKey(column)) return;
        errors[column] = errorText;
    }


    protected void gvwCompetition_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
    {
        DateTime Start_Date = Convert.ToDateTime(e.NewValues["Start_Date"]);
        DateTime End_Date = Convert.ToDateTime(e.NewValues["End_Date"]);

        if (e.NewValues["Competition_Name"] == null)
        {
            AddError(e.Errors, gvwCompetition.Columns["Competition_Name"], "Competition Name can't be null.");
        }

        if (e.NewValues["Competition_Detail"] == null)
            AddError(e.Errors, gvwCompetition.Columns["Competition_Detail"], "Details of competiton can't be null");

        if (e.NewValues["Start_Date"] == null)
            AddError(e.Errors, gvwCompetition.Columns["Start_Date"], "Start Date can't be null");
        else
        {
            if (e.OldValues["Start_Date"] == null)
            {
                if (Start_Date.Year < DateTime.Today.Year)
                {
                    AddError(e.Errors, gvwCompetition.Columns["Start_Date"], "Start Date must be at more than today");
                }
                else
                {
                    if (Start_Date.Year == DateTime.Today.Year && Start_Date.Month < DateTime.Today.Month)
                    {
                        AddError(e.Errors, gvwCompetition.Columns["Start_Date"], "Start Date must be at more than today");
                    }
                    else
                    {
                        if (Start_Date.Month == DateTime.Today.Month && Start_Date.Day < DateTime.Today.Day)
                        {
                            AddError(e.Errors, gvwCompetition.Columns["Start_Date"], "Start Date must be at more than today");
                        }
                    }
                }
            }            
        }

        // End date
        if (e.NewValues["End_Date"] == null)
            AddError(e.Errors, gvwCompetition.Columns["End_Date"], "End Date can't be null");
        else
        {
            if (e.OldValues["End_Date"] == null)
            {
                if (Start_Date.Year > End_Date.Year)
                {
                    AddError(e.Errors, gvwCompetition.Columns["End_Date"], "End Date must be at more than Start Date");
                }
                else
                {
                    if (Start_Date.Year == End_Date.Year && Start_Date.Month > End_Date.Month)
                    {
                        AddError(e.Errors, gvwCompetition.Columns["End_Date"], "End Date must be at more than Start Date");
                    }
                    else
                    {
                        if (Start_Date.Month == End_Date.Month && Start_Date.Day > End_Date.Day)
                        {
                            AddError(e.Errors, gvwCompetition.Columns["End_Date"], "End Date must be at more than Start Date");
                        }
                    }
                }
            }
        }

        if(e.NewValues["ClassID"] == null)
            AddError(e.Errors, gvwCompetition.Columns["ClassID"], "Class ID Can't be null");

        if (e.NewValues["CourseID"] == null)
            AddError(e.Errors, gvwCompetition.Columns["CourseID"], "Course ID Can't be null");
    }
}